
#define ADC_VAR	1

#include "adc_bsp.h"

#include "stdio.h"
#include "math.h"




void NTC_adc_init(void)
{
	    __RCU_APB2_CLK_ENABLE(RCU_APB2_PERI_GPIOA);
		__RCU_APB2_CLK_ENABLE(RCU_APB2_PERI_GPIOC);// Enable  GPIO clocks  
	
	gpio_mode_config(GPIOC,GPIO_PIN_1,GPIO_MODE_OUT_PP(GPIO_SPEED_HIGH));
	__GPIO_PIN_RESET(GPIOC, GPIO_PIN_1);
	
    gpio_mode_config(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_MODE_IN_ANALOG);
	
		 /* Configure ADC1 clock. */
    __RCU_APB2_CLK_ENABLE(RCU_APB2_PERI_ADC1);
    rcu_adcclk_config(RCU_ADCCLK_SEL_PCLK2_DIV8);
    
    adc_cfg_t ptr_cfg;
    
    /* Configure the basic information of ADC1. */
    __ADC_DEF_INIT(ADC1);
    adc_struct_init(&ptr_cfg);
    ptr_cfg.ext_trigger = ADC_EXT_TRIGGER_SWSTART;
    adc_init(ADC1, &ptr_cfg);
    
    /* ADC1 regular channel10 configuration. */
    adc_regular_channel_config(ADC1, ADC_CHANNEL_0, ADC_SAMPLE_TIME_55_5_CYCLE, 1);
    
    /* Enable ADC1. */
    __ADC_ENABLE(ADC1);
    
    /* Enable ADC1 reset calibration register. */
    __ADC_RESET_CALI(ADC1);
    /* Check the end of ADC1 reset calibration register. */
    while(__ADC_RESET_CALI_STATUS_GET(ADC1));
    
    /* Start ADC1 calibration. */
    __ADC_CALI_START(ADC1);
    /* Check the end of ADC1 calibration. */
    while(__ADC_CALI_STATUS_GET(ADC1));
		
    ADC1->CTR |=0x02;
}


//void nvic_config(void)
//{
//    nvic_init_t ptr_nvic;
//    
//    /* Configure and enable ADC interrupt. */
//    ptr_nvic.nvic_irqchannel = IRQn_ADC1_2;
//    ptr_nvic.nvic_irq_enable = ENABLE;
//    ptr_nvic.nvic_irq_pre_priority = 0;
//    ptr_nvic.nvic_irq_sub_priority = 0;
//    nvic_init(&ptr_nvic);
//}

uint16_t adc_sample_value(void)
{
	uint16_t value=0;
	
	 /* Start ADC1 Software Conversion. */
        __ADC_REG_CONV_START(ADC1);					//__ADC_INTR_ENABLE(ADCx, IT) 
        
        /* Wait for the conversion to complete. */
        while(RESET == __ADC_FLAG_STATUS_GET(ADC1, ADC_FLAG_EOC));
        
        /* Calculate the voltage value. */
        value = __ADC_CONV_VALUE_GET(ADC1);
//			 value = (uint16_t)((ADC1->JOUTDAT));
//        voltage = ((value * 3300) / 0xFFF);
	
	return value;
}

void GetAdcValue(void)//1ms call
{
		static uint16_t AdMaxValue=ADMAX;
		static uint16_t AdMinValue=ADMIN;
		static uint16_t AdMaxValue1=ADMAX;
		static uint16_t AdMinValue1=ADMIN;
		static uint16_t AdMaxValue2=ADMAX;
		static uint16_t AdMinValue2=ADMIN;
		static uint16_t AdMaxValue3=ADMAX;
		static uint16_t AdMinValue3=ADMIN;
		static uint16_t AdChannelBuf=0;
		for(AdChannelBuf=0;AdChannelBuf<5;AdChannelBuf++)
		{
				ADC1->SQR3=0;
				 adc_regular_channel_config(ADC1, AdChannelBuf, ADC_SAMPLE_TIME_55_5_CYCLE, 1);
				
				for(uint16_t i=0;i<100;i++)
				{
							__NOP();
				}
				 
				
				/* Start ADC1 Software Conversion. */
        __ADC_REG_CONV_START(ADC1);
        
        /* Wait for the conversion to complete. */
        while(RESET == __ADC_FLAG_STATUS_GET(ADC1, ADC_FLAG_EOC));
        
        /* Calculate the voltage value. */
       AdcValue[AdChannelBuf] = __ADC_CONV_VALUE_GET(ADC1);
		//	AdcValue[AdChannelBuf]= AdChannelBuf;

		}

		if(AdcValue[0]>AdMaxValue) AdMaxValue=AdcValue[0];
		if(AdcValue[0]<AdMinValue) AdMinValue=AdcValue[0];
		AdSum+=AdcValue[0];
		if(++AdSampleCnt>=10)
		{
				AdSampleCnt=0;
				AdSum-=AdMaxValue;
				AdSum-=AdMinValue;
				AdSum/=8;
				AdMaxValue=ADMAX;
				AdMinValue=ADMIN;
				PA0_Adc_Value=AdSum;
				AdSum=0;
		}
		
		NTC1_temp= value_to_temp(PA0_Adc_Value);
		
		
		if(AdcValue[1]>AdMaxValue1) AdMaxValue1=AdcValue[1];
		if(AdcValue[1]<AdMinValue1) AdMinValue1=AdcValue[1];
		AdSum1+=AdcValue[1];
		if(++AdSampleCnt1>=10)
		{
				AdSampleCnt1=0;
				AdSum1-=AdMaxValue1;
				AdSum1-=AdMinValue1;
				AdSum1/=8;
				AdMaxValue1=ADMAX;
				AdMinValue1=ADMIN;
				PA1_Adc_Value=AdSum1;
				AdSum1=0;
		}
		NTC2_temp= value_to_temp(PA1_Adc_Value);
		
		
		if(AdcValue[2]>AdMaxValue2) AdMaxValue2=AdcValue[2];
		if(AdcValue[2]<AdMinValue2) AdMinValue2=AdcValue[2];
		AdSum2+=AdcValue[2];
		if(++AdSampleCnt2>=10)
		{
				AdSampleCnt2=0;
				AdSum2-=AdMaxValue2;
				AdSum2-=AdMinValue2;
				AdSum2/=8;
				AdMaxValue2=ADMAX;
				AdMinValue2=ADMIN;
				PA2_Adc_Value=AdSum2;
				AdSum2=0;
		}
		NTC3_temp= value_to_temp(PA2_Adc_Value);
		
		
		
		if(AdcValue[3]>AdMaxValue3) AdMaxValue3=AdcValue[3];
		if(AdcValue[3]<AdMinValue3) AdMinValue3=AdcValue[3];
		AdSum3+=AdcValue[3];
		if(++AdSampleCnt3>=10)
		{
				AdSampleCnt3=0;
				AdSum3-=AdMaxValue3;
				AdSum3-=AdMinValue3;
				AdSum3/=8;
				AdMaxValue3=ADMAX;
				AdMinValue3=ADMIN;
				PA3_Adc_Value=AdSum3;
				AdSum3=0;
		}		
		NTC4_temp= value_to_temp(PA3_Adc_Value);
}



int8_t  value_to_temp(uint16_t adcsum)
{
	float  TVALUE;
	unsigned char adccnt=0;
	float  Rvalue1=0;
	float VARA,VARB;
	
		VARA = (float)adcsum;
	Rvalue1= RC*(VARA/(4095.0f-VARA));
	
			VARB = constB*(log(Rvalue1));	
			TVALUE = (((1/(constA + VARB)) -273.0f));		
		

	return (int8_t)TVALUE;
}
void ADC_IRQHandler(void)
{
//	if(adc_interrupt_flag_status_get()==SET)			// adc_auto_conversion_interrupt_flag_status_get
//	{
//		adc_interrupt_flag_clear();									// adc_auto_conversion_interrupt_flag_clear
//		adc_input_value=adc_conversion_result_get();
//			adc_conversion_start();
//	}
	
	//__ADC_FLAG_STATUS_GET(ADCx, FLAG)
	__ADC_FLAG_CLEAR(ADC1, ADC_FLAG_EOC);
}



